package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.address.TbAddress;
import com.dk.pojo.address.TbAreas;
import com.dk.pojo.address.TbCities;
import com.dk.pojo.address.TbProvinces;
import com.dk.pojo.user.TbUser;
import com.dk.service.address.AddressService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    private final Logger logger = LoggerFactory.getLogger(AddressController.class);

    @Reference
    private AddressService addressService;

    @RequestMapping("getAddressList")
    @ResponseBody
    public Result getAddressList() {
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            List<TbAddress> addresses = addressService.getAddressList(user.getId());
            return Result.response(ResultEnum.OK).setData(addresses);
        } catch (Exception e) {
            logger.error("获取地址信息错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("getProvince")
    @ResponseBody
    public Result getProvince() {
        try {
            List<TbProvinces> provincesList = addressService.getProvince();
            return Result.response(ResultEnum.OK).setData(provincesList);
        } catch (Exception e) {
            logger.error("获取省信息错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("getCities")
    @ResponseBody
    public Result getCities(String pid) {
        try {
            List<TbCities> citiesList = addressService.getCities(pid);
            return Result.response(ResultEnum.OK).setData(citiesList);
        } catch (Exception e){
            logger.error("获取市信息错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("getAreas")
    @ResponseBody
    public Result getAreas(String cid) {
        try {
            List<TbAreas> areasList = addressService.getAreas(cid);
            return Result.response(ResultEnum.OK).setData(areasList);
        } catch (Exception e){
            logger.error("获取县区信息错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("saveAddress")
    @ResponseBody
    public Result saveAddress(TbAddress address) {
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            address.setUserId(user.getId().toString());
            address.setCreateDate(new Date());
            int i = addressService.saveAddress(address);
            return Result.response(ResultEnum.OK);
        } catch (Exception e){
            logger.error("保存地址失败",e);
            return Result.response(ResultEnum.FAIL);

        }
    }
}
